package cn.yunhe.java.service;

import cn.yunhe.java.dao.SysRolesMapper;
import cn.yunhe.java.dao.SysUserMapper;
import cn.yunhe.java.object.TableData;
import cn.yunhe.java.pojo.SysRoles;
import cn.yunhe.java.pojo.SysUser;
import cn.yunhe.java.vo.ZTreeNode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private SysRolesMapper dao;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public TableData queryRolesByCondition(String roleName, Integer roleStatus, int page, int limit) {
        PageHelper.startPage(page,limit);
        Page<SysRoles> listPage = dao.queryByCondition(roleName,roleStatus);

        TableData td = new TableData();
        td.setCode(0);
        td.setMsg("OK");
        td.setCount(listPage.getTotal());
        td.setData(listPage.getResult());
        return td;
    }

    @Override
    public int updateRoleStatus(Integer roleId, Integer roleStatus) {
        SysRoles sr = new SysRoles();
        sr.setRoleId(roleId);
        sr.setRoleStatus(roleStatus);
        return dao.updateByPrimaryKeySelective(sr);
    }

    @Override
    public int saveRole(SysRoles su) {
        return dao.insertSelective(su);
    }

    @Override
    public int deleteRole(Integer roleId) {
        return dao.deleteByPrimaryKey(roleId);
    }

    @Override
    public SysRoles queryOne(Integer roleId) {
        return dao.selectByPrimaryKey(roleId);
    }

    @Override
    public int updateRoles(SysRoles sr) {
        return dao.updateByPrimaryKeySelective(sr);
    }

    @Override
    public List<ZTreeNode> createTree(Integer pMenuId,Integer roleId) {
        //1.查询权限项信息
       List<Map<String,Object>> rightsList = dao.queryRoleRights(pMenuId,roleId);

       //构造返回的List
        List<ZTreeNode> rtnList = new ArrayList<>();
        for(Map<String,Object> map : rightsList){
            ZTreeNode node = new ZTreeNode();
            node.setId(map.get("rights_id").toString()); //id
            node.setName(map.get("title").toString()); //节点的名字

            //判断有没有子节点的
            if(Integer.parseInt(map.get("childNum").toString())>0){
                node.setOpen(true);
               //递归
                List<ZTreeNode> subRightsList = createTree(Integer.parseInt(map.get("menu_id").toString()),roleId);
                node.setChildren(subRightsList);
            }
            //如果已经有该菜单的权限，则勾选
             node.setChecked(Integer.parseInt(map.get("isChecked").toString())>0);
            //页面上的按钮处理
            List<Map<String,Object>> btnList = dao.queryRightsBtn(Integer.parseInt(map.get("menu_id").toString()),roleId);

            /**
             * List: [{"rights_id":1,"item_name":"新增","isChecked":1}]
             * lambda  ==>  [{"rights_id":1,"item_name":"新增","isChecked":"checked"}]
             */
            List<Map<String,Object>> newList = btnList.stream().peek(map1 -> {
                  if(Integer.parseInt(map1.get("isChecked").toString())>0){
                      map1.put("isChecked","checked");
                  }
            }).collect(Collectors.toList());

            node.setModel(newList);

            rtnList.add(node);
        }
        return rtnList;
    }

    @Override
    public int saveRoleRights(Integer roleId, String[] rightsIds) {
        //1.删除原有的权限
          dao.deleteRoleRights(roleId);
        //2.新增勾选的权限
        int rst = dao.insertRoleRights(roleId,rightsIds);
        return rst;
    }

    @Override
    public List<Map<String, String>> queryAllRights(Integer userId) {
        return sysUserMapper.queryAllRights(userId);
    }
}
